package et.item.drill;

import et.common.ElectricThaum;
import ic2.api.item.ElectricItem;
import ic2.api.item.IBoxable;
import ic2.api.item.IElectricItem;
import ic2.api.item.IItemHudInfo;
import ic2.core.IC2;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.event.ForgeEventFactory;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * Created by winston_wang on 2017/6/9.
 */
public class ETItemDrill extends ItemTool implements IBoxable,IElectricItem,IItemHudInfo{
    protected int soundTicker = 0;
    public double operationEnergyCost;
    public int maxCharge;
    public int transferLimit;
    public int tier;
    public ETItemDrill(){
        this(0.0f, 50.0f, 30000, 100, 1, 3,8.0f);
    }
    public ETItemDrill(float damage, double operationEnergyCost, int maxCharge, int transferLimit, int tier, int level, float efficiencyOnProperMaterial) {
        super(damage, ToolMaterial.EMERALD, new HashSet());
        this.operationEnergyCost =operationEnergyCost;
        this.maxCharge =maxCharge;
        this.transferLimit =transferLimit;
        this.tier =tier;;
        setHarvestLevel("pickaxe",level);
        setHarvestLevel("shovel",level);
        this.efficiencyOnProperMaterial=efficiencyOnProperMaterial;
        setNoRepair();
        setCreativeTab(ElectricThaum.tabET);
        setMaxDamage(27);
    }
    @Override
    public float getDigSpeed(ItemStack stack, Block block, int meta) {
        float rStrength = getSpeed(stack, block, meta);
        if(rStrength > 1.0F) {
            ++this.soundTicker;
            if(this.soundTicker % 4 == 0) {
                IC2.platform.playSoundSp(this.getRandomDrillSound(), 1.0F, 1.0F);
            }
        }

        return rStrength;
    }
    public String getRandomDrillSound() {
        switch(IC2.random.nextInt(4)) {
            case 1:
                return "drillOne";
            case 2:
                return "drillTwo";
            case 3:
                return "drillThree";
            default:
                return "drill";
        }
    }
    public float getSpeed(ItemStack tool, Block block, int meta) {
        return !ElectricItem.manager.canUse(tool, this.operationEnergyCost)?1.0F:super.getDigSpeed(tool, block, meta);
    }
    @Override
    public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, Block block, int par4, int par5, int par6, EntityLivingBase par7EntityLiving) {
        if((double)block.getBlockHardness(par2World, par4, par5, par6) != 0.0D) {
            if(par7EntityLiving != null) {
                ElectricItem.manager.use(par1ItemStack, this.operationEnergyCost, par7EntityLiving);
            } else {
                ElectricItem.manager.discharge(par1ItemStack, this.operationEnergyCost, this.tier, true, false, false);
            }
        }

        return true;
    }
    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
        ElectricItem.manager.use(stack, 0.0D, player);
        return super.onItemRightClick(stack, world, player);
    }
    @Override
    public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float xOffset, float yOffset, float zOffset) {
        for(int i = 0; i < player.inventory.mainInventory.length; ++i) {
            ItemStack torchStack = player.inventory.mainInventory[i];
            if(torchStack != null && torchStack.getUnlocalizedName().toLowerCase(Locale.ENGLISH).contains("torch")) {
                Item item = torchStack.getItem();
                if(item instanceof ItemBlock) {
                    int oldMeta = torchStack.getItemDamage();
                    int oldSize = torchStack.stackSize;
                    boolean result = torchStack.tryPlaceItemIntoWorld(player, world, x, y, z, side, xOffset, yOffset, zOffset);
                    if(player.capabilities.isCreativeMode) {
                        torchStack.setItemDamage(oldMeta);
                        torchStack.stackSize = oldSize;
                    } else if(torchStack.stackSize <= 0) {
                        ForgeEventFactory.onPlayerDestroyItem(player, torchStack);
                        player.inventory.mainInventory[i] = null;
                    }

                    if(result) {
                        return true;
                    }
                }
            }
        }

        return super.onItemUse(stack, player, world, x, y, z, side, xOffset, yOffset, zOffset);
    }
    public float func_150893_a(ItemStack stack, Block block) {
        return this.canHarvestBlock(block, stack)?this.efficiencyOnProperMaterial:super.func_150893_a(stack, block);
    }
    @Override
    public void addInformation(ItemStack itemStack, EntityPlayer player, List info, boolean b) {
        info.add(StatCollector.translateToLocal("ic2.item.tooltip.PowerTier") + " " + this.tier);
    }
    @Override
    public List<String> getHudInfo(ItemStack itemStack) {
        LinkedList info = new LinkedList();
        info.add(ElectricItem.manager.getToolTip(itemStack));
        info.add(StatCollector.translateToLocal("ic2.item.tooltip.PowerTier") + " " + this.tier);
        return info;
    }
    @Override
    public boolean canBeStoredInToolbox(ItemStack itemstack) {return true;}
    @Override
    public boolean canProvideEnergy(ItemStack itemStack) {return false;}
    @Override
    public Item getChargedItem(ItemStack itemStack) {return this;}
    @Override
    public Item getEmptyItem(ItemStack itemStack) {return this;}
    @Override
    public double getMaxCharge(ItemStack itemStack) {return maxCharge;}
    @Override
    public int getTier(ItemStack itemStack) {return tier;}
    @Override
    public double getTransferLimit(ItemStack itemStack) {return transferLimit;}
    @Override
    public boolean isBookEnchantable(ItemStack itemstack1, ItemStack itemstack2) {return false;}
}
